import java.util.Arrays;

public class HeapSort {
    public static void main(String[] args) {
        int [] array = {103,23,45,12,67,123,89,90,4};
        heapSort(array);
        System.out.println(Arrays.toString(array));
    }

    private static void heapSort(int[] array) {
        createBigHeap(array);//时间复杂度O(n)
        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for(int parent = (array.length-1-1)/2;parent >= 0;parent--){
            shiftDown(array,parent,array.length);
        }
        System.out.println(Arrays.toString(array));
    }

    private static void shiftDown(int[] array, int parent, int length) {
        int child = 2*parent+1;
        while (child < length){
            if(child +1 <length && array[child+1] > array[child]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }

    }

    private static void swap(int[] array, int child, int parent) {
        int tmp = array[child];
        array[child] = array[parent];
        array[parent] = tmp;
    }
}
